package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.FinanceTxnMapper;
import cc.rengu.oltp.service.common.entity.FinanceTxn;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 交易流水表操作接口实现类
 * Created by 王成 on 2018/4/18.
 */
public class FinanceTxnMapperImpl implements FinanceTxnMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public FinanceTxnMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public int insertFinanceTxn(FinanceTxn financeTxn) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertFinanceTxn");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, financeTxn, null);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("登记金融交易流水表失败,instId:<{}>,sysSeqNum:<{}>,txnDate:<{}>,txnTime:<{}>,txnNum:<{}>,msgSrcId:<{}>,transChannelId:<{}>,frontSeqNum:<{}>,iReturnCode:<{}>",
                    financeTxn.getInstId(), financeTxn.getSysSeqNum(), financeTxn.getTxnDate(), financeTxn.getTxnTime(), financeTxn.getTxnNum(), financeTxn.getMsgSrcId(), financeTxn.getTransChannelId(), financeTxn.getFrontSeqNum(), iReturnCode);
            return iReturnCode;
        }
        return Database.DBS_SUCCESS;
    }

    @Override
    public int updateFinanceTxnByPrimaryKey(FinanceTxn financeTxn) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateFinanceTxnByPrimaryKey");
        financeTxn.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, financeTxn, null);
        if (Database.DBS_NOT_FOUND == iReturnCode) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateFinanceTxnHisByPrimaryKey");
            iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, financeTxn, null);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("更新金融交易流水表失败,instId:<{}>,sysSeqNum:<{}>,txnDate:<{}>,txnTime:<{}>,txnNum:<{}>,msgSrcId:<{}>,transChannelId:<{}>,frontSeqNum:<{}>，iReturnCode:<{}>",
                        financeTxn.getInstId(), financeTxn.getSysSeqNum(), financeTxn.getTxnDate(), financeTxn.getTxnTime(), financeTxn.getTxnNum(), financeTxn.getMsgSrcId(), financeTxn.getTransChannelId(), financeTxn.getFrontSeqNum(), iReturnCode);
                return iReturnCode;
            }
        }
        return Database.DBS_SUCCESS;
    }

    @Override
    public FinanceTxn selectFinanceTxnInfoByFrontSeqNum(String instId, String transDateTime, String frontSeqNum, String msgSrcId, String transChannelId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectFinanceTxnInfoByFrontSeqNum");
        FinanceTxn financeTxn = new FinanceTxn();
        financeTxn.setInstId(instId);
        financeTxn.setTransDateTime(transDateTime);
        financeTxn.setFrontSeqNum(frontSeqNum);
        financeTxn.setMsgSrcId(msgSrcId);
        financeTxn.setTransChannelId(transChannelId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, financeTxn, financeTxn);
        if (Database.DBS_NOT_FOUND == iReturnCode) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectFinanceTxnInfoHisByFrontSeqNum");
            iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, financeTxn, financeTxn);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.warn("根据前端流水号查询交易流水表失败,instId:<{}>,transDateTime:<{}>,frontSeqNum:<{}>,msgSrcId:<{}>,transChannelId:<{}>,iReturnCode:<{}>", instId, transDateTime, frontSeqNum, msgSrcId, transChannelId, iReturnCode);
                return null;
            }
        }
        return financeTxn;
    }

    @Override
    public FinanceTxn selectFinanceTxnInfoByPrimaryKey(String instId, String sysSeqNum) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectFinanceTxnInfoByPrimaryKey");
        FinanceTxn financeTxn = new FinanceTxn();
        financeTxn.setInstId(instId);
        financeTxn.setSysSeqNum(sysSeqNum);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, financeTxn, financeTxn);
        if (Database.DBS_NOT_FOUND == iReturnCode) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectFinanceTxnInfoHisByPrimaryKey");
            iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, financeTxn, financeTxn);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.warn("根据系统流水号查询交易流水表失败,instId:<{}>,sysSeqNum:<{}>,iReturnCode:<{}>", instId, sysSeqNum, iReturnCode);
                return null;
            }
        }
        return financeTxn;
    }
}
